package com.lagou.dao.impl;

import com.lagou.dao.CourseContentDao;
import com.lagou.pojo.Course;
import com.lagou.pojo.Course_Lesson;
import com.lagou.pojo.Course_Section;
import com.lagou.utils.DateUtils;
import com.lagou.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class CourseContentDaoImpl implements CourseContentDao {
    @Override
    public List<Course_Section> findSectionAndLessonByCourseId(int courseId) {

        List<Course_Section> sectionList = null;
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

            String sql = "SELECT id,course_id,section_name, description,order_num,STATUS " +
                    "FROM course_section WHERE course_id = ?";
            sectionList = qr.query(sql, new BeanListHandler<Course_Section>(Course_Section.class), courseId);

            for (Course_Section section : sectionList) {
                List<Course_Lesson> lessonList = findLessonBySectionId(section.getId());
                section.setLessonList(lessonList);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return sectionList;
    }

    @Override
    public List<Course_Lesson> findLessonBySectionId(int sectionId) {

        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

            String sql = "SELECT id,course_id,section_id,theme, duration, is_free,order_num, STATUS FROM course_lesson WHERE section_id=?";

            List<Course_Lesson> lessonList = qr.query(sql, new BeanListHandler<Course_Lesson>(Course_Lesson.class), sectionId);
            return lessonList;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public Course findCourseByCourseId(int courseId) {

        Course course = null;
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

            String sql = "select id, course_name from course where id = ?";

            course = qr.query(sql, new BeanHandler<Course>(Course.class), courseId);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return course;
    }

    @Override
    public int saveSection(Course_Section section) {

        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

            String sql = "insert into course_section (course_id, section_name, description, order_num, status, " +
                    "create_time, update_time) values (?, ?, ?, ?, ?, ?, ?)";

            Object[] params = {
                    section.getCourse_id(), section.getSection_name(), section.getDescription(),
                    section.getOrder_num(), section.getStatus(), section.getCreate_time(), section.getUpdate_time()
            };
            int row = qr.update(sql, params);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }

    @Override
    public int updateSection(Course_Section section) {

        int row = 0;
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

            String sql = "update course_section set section_name=?, description=?,order_num=?,update_time=? where id=?";

            Object[] params = {
                    section.getSection_name(),
                    section.getDescription(),
                    section.getOrder_num(),
                    section.getUpdate_time(),
                    section.getId()
            };

            row = qr.update(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }

    @Override
    public int updateSectionStatus(int id, int status) {

        int row = 0;
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

            String sql = "update course_section set status = ?, update_time = ? where id = ?";

            Object[] params = {status, DateUtils.getDateFormat(), id};

            row = qr.update(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return row;
    }

    @Override
    public int saveLesson(Course_Lesson lesson) {

        int row = 0;

        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

            String sql = "insert into course_lesson (course_id, section_id, theme, duration, is_free, order_num, status," +
                    "create_time,update_time) values (?, ?, ?, ?, ?, ?, ?, ?, ?)";
            Object[] params = {
                    lesson.getCourse_id(),
                    lesson.getSection_id(),
                    lesson.getTheme(),
                    lesson.getDuration(),
                    lesson.getIs_free(),
                    lesson.getOrder_num(),
                    lesson.getStatus(),
                    lesson.getCreate_time(),
                    lesson.getUpdate_time()
            };

            row = qr.update(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }

    @Override
    public int updateLesson(Course_Lesson lesson) {

        int update = 0;
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

            String sql = "update course_lesson set course_id = ?, section_id = ?, theme = ?, duration = ?, is_free = ?, " +
                    "order_num = ?, update_time = ? where id = ?";
            Object[] params = {
                    lesson.getCourse_id(),
                    lesson.getSection_id(),
                    lesson.getTheme(),
                    lesson.getDuration(),
                    lesson.getIs_free(),
                    lesson.getOrder_num(),
                    lesson.getUpdate_time(),
                    lesson.getId()
            };
            update = qr.update(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return update;
    }
}
